<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item>
        <el-button
          icon="refresh"
          color=" rgb(69, 76, 105)"
          style="color: #fff"
          @click="resetQuery()"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          @click="handleAdd"
          v-hasPermi="['system:Combo:add']"
          style="padding: 0 20px"
        >
          新 增
        </el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="comboList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        label="套餐名称"
        align="center"
        prop="comboName"
      />
      <el-table-column
        label="时间/天"
        align="center"
        prop="comboTime"
      />
      <el-table-column
        label="现价/元"
        align="center"
        prop="nowPrice"
      />
      <el-table-column
        label="原价/元"
        align="center"
        prop="originPrice"
      />
      <el-table-column
        label="兑换/贡献分"
        align="center"
        prop="needContribute"
      />
      <el-table-column
        label="状态"
        align="center"
        prop="state"
      >
        <template #default="scope">
          <el-switch
            v-model="scope.row.state"
            :active-value="'1'"
            :inactive-value="'0'"
            @change="handleState(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:Combo:edit']"
            >编辑</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:Combo:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      :title="title"
      v-model="open"
      width="500px"
      append-to-body
      :before-close="cancel"
    >
      <el-form
        ref="comboRef"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item
          label="套餐名称"
          prop="brandImg"
        >
          <el-input
            v-model="form.comboName"
            placeholder="请输入套餐名称"
          >
          </el-input>
        </el-form-item>
        <el-form-item
          label="套餐时间"
          prop="brandImg"
        >
          <el-input
            v-model="form.comboTime"
            placeholder="请输入套餐时间"
            style="width:350px"
          >
          </el-input>天
        </el-form-item>
        <el-form-item
          label="现价/元"
          prop="brandImg"
          ><el-input
            v-model="form.nowPrice"
            placeholder="请输入现价"
          >
          </el-input>
        </el-form-item>
        <el-form-item
          label="原价/元"
          prop="brandImg"
          ><el-input
            v-model="form.originPrice"
            placeholder="请输入原价"
          >
          </el-input
        ></el-form-item>
        <el-form-item
          label="贡献分"
          prop="brandImg"
          ><el-input
            v-model="form.needContribute"
            placeholder="请输入贡献分"
          >
          </el-input
        ></el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button
            type="primary"
            @click="submitForm"
            >确 定</el-button
          >
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Combo">
import { listCombo, delCombo, addCombo, updateCombo } from "@/api/system/combo";
const { proxy } = getCurrentInstance();

const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const comboList = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  },
  rules: {},
});
const { queryParams, form, rules } = toRefs(data);

/** 查询车辆管理列表 */
function getList() {
  loading.value = true;
  listCombo(queryParams.value).then((response) => {
    comboList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

function handleAdd() {
  form.value = {};
  open.value = true;
  title.value = "添加套餐";
}

function cancel() {
  open.value = false;
  form.value = {};
  done();
}

function handleState(row) {
  form.value = row;
  updateCombo(form.value).then((response) => {
    proxy.$modal.msgSuccess("修改成功");
    getList();
  });
}

function handleUpdate(row) {
  form.value = row;
  open.value = true;
  title.value = "修改套餐";
}

function submitForm() {
  proxy.$refs["comboRef"].validate((valid) => {
    if (valid) {
      if (form.value.comboId != null) {
        updateCombo(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addCombo(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          data.file = null;
          getList();
        });
      }
    }
  });
}

function handleDelete(row) {
  const _comboIds = row.comboId || ids.value;
  proxy.$modal
    .confirm('是否确认删除套餐编号为"' + _comboIds + '"的数据项？')
    .then(function () {
      return delCombo(_comboIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}
getList();
</script>
